package org.example.ims.DTO;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.time.LocalDateTime;

@JsonInclude(JsonInclude.Include.NON_NULL) // 只序列化非空字段
@Data
public class Response<T> {
    private String code;
    private String message;
    private T data;
    private LocalDateTime timestamp;

    // 1. 提供带默认逻辑的重载方法（不传 message 时用 "操作成功"）
    public static <T> Response<T> success(T data) {
        return success(data, "操作成功");
    }

    public static <T> Response<T> success(T data, String message ) {
        Response<T> response = new Response<>();
        response.setCode("200");
        response.setMessage(message);
        response.setData(data);
        response.setTimestamp(LocalDateTime.now());
        return response;
    }

    public static <T> Response<T> error(String code, String message) {
        Response<T> response = new Response<>();
        response.setCode(code);
        response.setMessage(message);
        response.setData(null); // 或者直接省略这个字段
        response.setTimestamp(LocalDateTime.now());
        return response;
    }
}